package com.future.mayi.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.activity.BasePage;
import com.future.mayi.adapter.BasePageAdapter;
import com.future.mayi.app.App;
import com.future.mayi.fragment.GoodsImagePage;
import com.future.mayi.fragment.GoodsVideoPage;
import com.future.mayi.model.interfaces.OnItemPageClick;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 周大虎 on 2017/3/4.
 */

public class GoodsTitleView extends LinearLayout implements View.OnClickListener {


    private ViewPager vpIndex;
    private TextView tvDian;


    private int pageSize = 0;

    private Context mContext;


    public GoodsTitleView(Context context) {
        super(context);
        init(context);
    }

    public GoodsTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public GoodsTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private OnItemPageClick onItemPageClick;


    private void init(Context context) {
        mContext = context;
        View.inflate(context, R.layout.view_title_imgview, this);
        vpIndex = findViewById(R.id.vp_index);
        tvDian = findViewById(R.id.tv_dian);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) vpIndex.getLayoutParams();
        layoutParams.width = CommonUtils.getScreenSize(mContext)[0];
        layoutParams.height = (int) ((float) layoutParams.width);
    }

    private List<String> list;


    public void setOnItemPageClick(OnItemPageClick onItemPageClick) {
        this.onItemPageClick = onItemPageClick;
    }

    private List<BasePage> indexCarouselPageList;

    public void setData(List<String> list, final String main_video, String main_video_img) {

        if (this.list != null && App.getGson().toJson(list).equals(App.getGson().toJson(this.list)))
            return;
        this.list = list;


        indexCarouselPageList = new ArrayList<>(10);

        if (!TextUtils.isEmpty(main_video)) {
            GoodsVideoPage videoPage = new GoodsVideoPage(mContext);
            videoPage.setData(main_video, main_video_img);
            indexCarouselPageList.add(videoPage);
        }


        GoodsImagePage carouselPage;
        if (list == null && indexCarouselPageList.size() == 0) {
            pageSize = 0;
            carouselPage = new GoodsImagePage(mContext);
            carouselPage.setData(null);
            indexCarouselPageList.add(carouselPage);
        } else {
            pageSize = list.size();
            for (int i = 0; i < pageSize; i++) {
                carouselPage = new GoodsImagePage(mContext);
                carouselPage.setData(list.get(i));
                indexCarouselPageList.add(carouselPage);
                carouselPage.getView().setTag(i);
                carouselPage.getView().setOnClickListener(this);
            }
        }

        vpIndex.setAdapter(new BasePageAdapter(indexCarouselPageList, new ArrayList<String>()));
        vpIndex.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (!TextUtils.isEmpty(main_video)) GSYVideoManager.releaseAllVideos();
                tvDian.setText((position + 1) + "/" + indexCarouselPageList.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        tvDian.setText("1/" + indexCarouselPageList.size());

    }


    @Override
    public void onClick(View v) {
        if (v.getTag() != null && v.getTag() instanceof Integer) {
            if (onItemPageClick != null)
                onItemPageClick.onItemPage((Integer) v.getTag());
        }
    }


}
